<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="70%" destroy-on-close @closed="$emit('closed')" top="10vh" :align-center="true">
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="left" style="height:680px;overflow:auto">
                    <el-row :gutter="1">
                        <el-col :span="12">
                            <el-form-item label="吓数资料">
                                <scTable ref="tableXia" :data="xiaData" stripe border hidePagination hideDo height="270">
                                    <el-table-column type="index" align="center" width="50"></el-table-column>
                                    <el-table-column prop="date" label="更新日期" align="center" width="120"></el-table-column>
                                    <el-table-column prop="creator_name" label="更新人" align="center" width="120"></el-table-column>
                                    <el-table-column prop="detail" label="吓数资料" align="center" width="120">
                                        <template #default="scope">
                                            <div class="shopInfo">
                                                <el-image class="img" :src="scope.row.detail[0]" :preview-src-list="scope.row.detail" :initial-index="0" fit="cover" preview-teleported="true" />
                                            </div>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="操作" align="center">
                                        <template #default="scope">
                                            <el-button-group style="display:flex;flex-direction:column">
                                                <el-button text type="primary" size="small" @click="loadXia(scope.row, scope.$index)">下载</el-button>
                                                <el-button text type="danger" size="small" @click="delXia(scope.row, scope.$index)">删除</el-button>
                                            </el-button-group>
                                        </template>
                                    </el-table-column>
                                </scTable>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <sc-upload-multiple v-model="form.paper_sample_url" @file="before">
                                <el-button type="primary" size="small">上传</el-button>
                            </sc-upload-multiple>
                        </el-col>
                    </el-row>
                    <!-- <el-col :span="24">
                        <div style="display:flex;justify-content: end">
                            <el-button type="primary" size="small" @click="upLoadXia">保存吓数资料</el-button>
                        </div>
                    </el-col> -->
                    <el-row :gutter="1">
                        <el-col :span="12">
                            <el-form-item label="画花资料">
                                <scTable ref="tableHua" :data="huaData" stripe border hidePagination hideDo height="270">
                                    <el-table-column type="index" align="center" width="50"></el-table-column>
                                    <el-table-column prop="date" label="更新日期" align="center" width="120"></el-table-column>
                                    <el-table-column prop="creator_name" label="更新人" align="center" width="120"></el-table-column>
                                    <el-table-column prop="file_name" label="画花资料" align="center" width="120"></el-table-column>
                                    <el-table-column label="操作" align="center">
                                        <template #default="scope">
                                            <el-button-group style="display:flex;flex-direction:column">
                                                <el-button text type="primary" size="small" @click="loadHua(scope.row, scope.$index)">下载</el-button>
                                                <el-button text type="danger" size="small" @click="delHua(scope.row, scope.$index)">删除</el-button>
                                            </el-button-group>
                                        </template>
                                    </el-table-column>
                                </scTable>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <sc-upload-file v-model="form.ziliao_url" :limit="1" @file="before">
                                <el-button type="primary" size="small">上传</el-button>
                            </sc-upload-file>
                        </el-col>
                    </el-row>
                    <!-- <el-col :span="24">
                        <div style="display:flex;justify-content: end">
                            <el-button type="primary" size="small" @click="upLoadHua">保存画花资料</el-button>
                        </div>
                    </el-col> -->
                </el-form>
            </el-main>
            <el-footer>
                <div style="display:flex;justify-content: end;">
                    <el-button @click="visible = false">取 消</el-button>
                    <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
                </div>
            </el-footer>
        </el-container>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增商品',
				edit: '资料管理',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				ziliao_url: "",
				ziliao_url_name: "",
                paper_sample_url:"",
                is_supplier:1
			},
            // forms: {
			// 	id: "",
            //     paper_sample_url:"",
			// },
            xiaData:[],
            huaData:[],
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        before(file){
            this.form.ziliao_url_name = file.name
        },
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            // this.forms.id = data.id
            var res = await this.$API.goods.getGoodsFileDetail.get({id:data.id,is_supplier:1});
            this.xiaData = res.data.image_list;
            this.huaData = res.data.ziliao_list;
		},
        submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
                    res = await this.$API.goods.saveZiliao.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        // 上传吓数
        async upLoadXia(){
            this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
                    res = await this.$API.goods.addPaperSample.post(this.forms);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.forms, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
        },
        // 上传画花
        async upLoadHua(){
            this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
                    res = await this.$API.goods.addZiliao.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
        },
        async delXia(row,index){
            var reqData = { id: row.id ,type:1 }
			var res = await this.$API.goods.delZiliao.post(reqData);
			if (res.code == 1) {
				this.$refs.tableXia.tableData.splice(index, 1);
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
        },
        async delHua(row,index){
            var reqData = { id: row.id ,type:2 }
			var res = await this.$API.goods.delZiliao.post(reqData);
			if (res.code == 1) {
				this.$refs.tableHua.tableData.splice(index, 1);
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
        },
        loadHua(row){
            const fileUrl = row.file;
            const link = document.createElement("a");
            link.href = fileUrl;
            link.download = row.file;
            link.click();
        },
        loadXia(row){
            const fileUrl = row.file;
            const link = document.createElement("a");
            link.href = fileUrl;
            link.download = row.file;
            link.click();
        },
	}
}
</script>

<style>
</style>
